/** @QFi-template-author: wanghua
 ** @path view/vecspace/middleplatform/filemanagementpart/uploadFileStorage/uploadFileStorage_list.js  view/vecspace/middleplatform/filemanagementpart/uploadFileStorage/uploadFileStorage_list.ftl
 ** @url  api/view/middleplatform_uploadFileStorageHandler/list
 ** @class UploadFileStorageHandler.list()
 **/ 
var laytable;
var grid1;
var dkquery;
$(function() {
	laytable = layui.table;
	//年选择器
	  laydate.render({
	    elem: '#year'
	    ,type: 'year'
	    ,max: '2020'
	    ,btns: ['confirm']
	  	,value: new Date()
	  });
	dkquery = $('#dkQueryPanel').dkQuery2();
	$('input').iCheck(); //$('#xxx').iCheck('check')	$('#xxx').iCheck('uncheck');
	$('input').on('ifChanged', function(event){ //checkbox或者radio输入框的 checked 或 disabled 状态改变了，由iCheck组件触发

	});
    $("#year").val((new Date()).getFullYear());
	initDataGrid();
	onBtnQueryClick();
});

$(window).load(function() {
	$("#btnDkQuery").focus(); //默认让查询按钮获取焦点，防止第一个获取的焦点是一个dkSelectTree类型的控件，会自动列出下拉列表。
});

function onBtnQueryClick(){
	var queryFields = dkquery.getQueryParams();
	reloadGridData(queryFields);
}

function onGetQueryParams(){
	var params = dkquery.getQueryParams();
	alert(JSON.stringify(params));
}

function initDataGrid(){
	grid1 = laytable.render({
		id:'maingrid1',
		elem:'#maingrid1',
		url: basePath+'api/base/middleplatform_uploadFileRecordHandler/getUploadFileRecordExList?js_plugin_type=laytable',
		cellMinWidth: 80, 
		delayLoad : true,		//true:在初始化表格时不加载数据
		cols: [[/*sort:false,fixed:false|'right',rowspan:1,colspan:1,render:function(row, rowindex, rows) */
			{type:'numbers', width:30},	/*行号*/
			/*{title:'访问ID', field:'accessId',type:'normal',align:'center', width:'270',render: function(row){
				var idstr = row.id;
				var html = '<a style=' + "'color: #0099CD'" + ' href='+ basePath + "api/file/mps_filehandler/downloadFile?id=" + idstr + '>'+ idstr +'</a>';
				return html;
			}},*/
			{title:'原文件名', field:'orgFileName',type:'normal',align:'center',width:'280',render: function(row){
				var idstr = row.id;
				var html = '<a style=' + "'color: #0099CD'" + ' href='+ basePath + "api/file/mps_filehandler/downloadFile?id=" + idstr + '>'+ row.orgFileName +'</a>';
				return html;
			}},
			{title:'路径名称', field:'storageName',type:'normal',width:'270',align:'center',render:function(row){
			  var idstr = "'"+row.storageId+"'";
			  return '<a href="javascript:onStorageNameClick(' + idstr + ')" class="grid_row_btn grid_row_info">'+row.storageName+'</a>'
			}},
			{title:'文件字节数', field:'fileBytes',type:'normal',align:'center',width:'120',render: function(row){
				var size = row.fileBytes;
				var rest = 0;
				if(size < 1000){
					return size + "B";
				}else{
					size /= 1024.0;
				}

				if(size < 1000){
					size = size.toFixed(2)
					return size + "KB";
				}else{
					//rest = size % 1024;
					size /= 1024.0;
				}

				if(size < 1000){
					size = size.toFixed(2)
					return size + "MB";
					//size = size * 100;
					//return size / 100 + "." + rest * 100 / 1024 % 100 + "MB";
				}else{
					size = size / 1024.0;
					size = size.toFixed(2)
					return size + "GB";
				}
			}},
//			{title:'文件后缀名（小写', field:'fileExt',type:'normal',align:'center'},
			{title:'文件上传人', field:'uploader',type:'normal',align:'center'},
			{title:'上传时间', field:'uploadTime',type:'normal',align:'center',width:'150',render: function(row){
				var updateTime = new Date(row.uploadTime);
				 /*重载方法*/
			    Date.prototype.toLocaleString = function() {
			          return this.getFullYear() + "-" + (this.getMonth() + 1) + "-" + this.getDate() + " " + this.getHours() + ":" + this.getMinutes() + ":" + this.getSeconds();
			    };
				return updateTime.toLocaleString()
			}},
			{title:'所属应用系统', field:'appId',type:'normal',align:'center'},
			{title:'所属租户', field:'tenantId',type:'normal',align:'center'},
			{title:'访问次数', field:'accessTimes',type:'normal',align:'center'},
			{title:'文件分类码1', field:'classifyCode1',type:'normal',width:'150',align:'center'},
			{title:'文件分类码2', field:'classifyCode2',type:'normal',width:'150',align:'center'},
			/*{title:'缩略图1高度', field:'thumbnail1Height',type:'normal',align:'center'},
			{title:'缩略图1宽度', field:'thumbnail1Width',type:'normal',align:'center'},
			{title:'缩略图1字节数', field:'thumbnail1Bytes',type:'normal',align:'center'},
			{title:'缩略图2高度', field:'thumbnail2Height',type:'normal',align:'center'},
			{title:'缩略图2宽度', field:'thumbnail2Width',type:'normal',align:'center'},
			{title:'缩略图2字节数', field:'thumbnail2Bytes',type:'normal',align:'center'},
			{title:'备注', field:'remarks',type:'normal',align:'center'},*/			
            {title:'存储路径（含文件名）', field:'filePath',type:'normal',width:'300',align:'center'},

		]],
		page : true,
		limit: 15,
		heightDiff : 0,
		even: true, //开启隔行背景，
		trackSelectedRow:true,//选中行高亮显示 .layui-table-row-active
		loading : false,//true：显示加载数据的loading
		onLoadData : function(data){//当从后台加载到数据的时候触发

		},
		onRowClick : function(index, row){//当点击一行的时候触发
			//console.log(index+':'+JSON.stringify(row));
		},
		onRowDblClick : function(index, row){//当双击一行的时候触发
			//console.log(index+':'+JSON.stringify(row));
		}
	});
}

function reloadGridData(queryFields){
	grid1.setRequestParam('queryFields', JSON.stringify(queryFields));
	grid1.setRequestParam('year', $("#year").val());
	grid1.changePage(1);
	grid1.reloadData();
}

function reloadCurrPageGridData(){
	grid1.reloadData();
}

function onGetGridData(gridData){
	if(gridData.hasError){
		alert(gridData.errorMessage);
		return false;
	}
}
var editDlgWidth = 800;
var editDlgHeight = 452;
var childDlg;

function onItemInfoClick(id){
	var url = basePath+"api/view/middleplatform_uploadFileStorageHandler/info?id="+id;
	var dlgparams = {title:'文件存储路径 详细信息', url: url, id:'info_TMS_UPLOAD_FILE_STORAGE', width:700, height: 400, modal:true};
	childDlg = dialogframe.openDialog(dlgparams);
}

function onStorageNameClick(storageId){
    var url = basePath+"api/view/middleplatform_uploadFileStorageHandler/info?id="+storageId;
    var dlgparams = {title:'文件存储路径 详细信息', url: url, id:'info_TMS_UPLOAD_FILE_STORAGE', width:700, height: 400, modal:true};
    childDlg = dialogframe.openDialog(dlgparams);
}

function btnAddClick(){
	var url = basePath+'api/view/middleplatform_uploadFileStorageHandler/add';
	var dlgparams = {title:'添加 文件存储路径', url: url, id:'frm_addUploadFileStorage', width:editDlgWidth, height: editDlgHeight, modal:true};
	childDlg = dialogframe.openDialog(dlgparams);
	childDlg.bind('Close', function(){  //点击右上角关闭按钮时触发的事件，如果返回false，则终止关闭.
		//alert("before close");
		return true;
	});
	childDlg.bind('Closed', function(event){  //窗口关闭后触发的事件，event.type:'added' 或者空值,event.dataId,新增数据的Id
		if(event && 'added' == event.type){
			reloadCurrPageGridData();
		}
	});
}

function onEditInfoClick(id){
	var url = basePath+"api/view/middleplatform_uploadFileStorageHandler/edit?id="+id;
	var dlgparams = {title:'编辑 文件存储路径', url: url, id:'frm_editUploadFileStorage', width:editDlgWidth, height: editDlgHeight, modal:true};
	childDlg = dialogframe.openDialog(dlgparams);
	childDlg.bind('Close', function(){  //点击右上角关闭按钮时触发的事件，如果返回false，则终止关闭.
		//alert("before close");
		return true;
	});
	childDlg.bind('Closed', function(event){  //窗口关闭后触发的事件，	  event.type:'edited' 或者空值,event.dataId,新增数据的Id
		if(event && 'edited' == event.type){
			reloadCurrPageGridData();
		}
	});
}

function onDeleteClick(id,name){
	if(!name) name = '';
	var layer = layui.layer;
	layer.confirm('确认要删除['+name+']吗？', 
		{ btn: ['确认','取消'] }, 
		function(layerIdex){//确认
			var url = basePath + 'api/base/middleplatform_uploadFileStorageHandler/deleteUploadFileStorage';
			$.post(url,{'id':id},  function(data, textStatus, jqXHR){
				if(data.hasError){
					alert(data.errorMessage);
				}else{
					reloadCurrPageGridData();
				}
			});
			layer.close(layerIdex);
		}, 
		function(){//取消

		}
	);	
}

